package com.example.jetpack.b_viewModel_livedata.livedata

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.ViewModelProvider
import com.example.jetpack.R
import com.example.jetpack.databinding.ActivityBABinding
import java.util.Random

class BAActivity : AppCompatActivity() {

    private lateinit var binding: ActivityBABinding

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityBABinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        val viewModel = ViewModelProvider(this)[UserViewModel::class.java]

        viewModel.userName.observe(this) { userName ->
            binding.userName.text = userName
        }
        binding.getUserBtn.setOnClickListener {
            val userId = Random().nextInt(100)
            viewModel.getUser(userId)
        }

        viewModel.user.observe(this) { user ->
            binding.userId.text = "userId=${user.id}"
        }
    }
}